<?php
/**
 * Created by PhpStorm.
 * User: ulinix
 * Date: 2017/10/12
 * Time: 18:53
 */
namespace Home\Controller;
use Think\Controller;
class Home2017Controller extends Controller {
    private static $url=".shtml";
    //
    public static function menu($catid,$where=''){
        //qong tur
        $map=array();
        $row=M("menu")->where("status=1 and pid=0 and catid=".$catid .$where)
            ->field("id,name,status,url,intime,sort,pid")
            ->order('sort asc')
            ->select();
        foreach ($row as $k=>$v){
            if(empty($v['url'])){
                $v['url']='index'.self::$url;
            }else{
                $v['url']=$v['url'].self::$url;
            }
            $map[]=$v;
        }
        return $map;
    }
    public static function guzal_suez($catid,$where='',$limit0=0,$limit1=10){
        //qong tur
        $map=array();
        $row=M("menu")->where("status=1 and catid=".$catid .$where)
            ->field("id,name,status,url,intime,sort,icon")
            ->order('sort asc')
            ->limit($limit0,$limit1)
            ->select();
        foreach ($row as $k=>$v){
            if(empty($v['url'])){
                $v['url']="/view/".date("Y/m/",strtotime($v['intime'])).$v['id'].self::$url;
            }else{
                $v['is_url']=1;
            }
            $map[]=$v;
        }
        return $map;
    }
    public static function banner_ad($pid,$where='',$limit0=0,$limit1=''){
        //qong tur
        $map=array();
        $row=M("bannerad")->where("status=1 and pid=$pid" .$where)
            ->limit($limit0,$limit1)
            ->field("id,adtitle,status,thumb,intime")
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/view/".date("Y/m/",strtotime($v['intime'])).$v['id'].self::$url;
            $map[]=$v;
        }
        return $map;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    //pid ha asasn hawar qikirix
    public static function home_news($pid,$where='',$limit0='',$limit1='',$fields='',$mobile_limit1='',$order='intime desc'){
        if(isMobile() and $mobile_limit1!=''){
            $limit1=$mobile_limit1;
        }
        $map=array();
        //获取所有子分类ID hamma kiqik sahipingi ID siha erixidu
        $catids=Home2018Controller::getAllChildcateIds($pid);
        if($catids){
            $row=M("article")->where("status=1 and pid IN ($catids) " .$where)
                ->field($fields)
                ->order($order)
                ->limit($limit0,$limit1)
                ->select();
        }else{
            $row=M("article")->where("status=1 and pid=".$pid .$where)
                ->field($fields)
                ->order($order)
                ->limit($limit0,$limit1)
                ->select();
        }
        foreach ($row as $k=>$v){
            $v['url']="/view/".date("Y/m/",$v['intime']).$v['id'].self::$url;
            $v['tag']=self::news_tag($v['id']);
            $map[]=$v;
        }
        return $map;
    }
    /////////////////////////////////////////////////////////////////////////////
    
    //kuerux bat
    public static function news_view($id,$where=''){
        //qong tur
        $row=M("article")
            ->where("status=1 and id='$id' ".$where)
            ->find();
        $pid=M("article")->where("id='$id'")->getField("pid");
        if($pid){
            $row['catids']=explode(",",Home2018Controller::getAllFcateIds($pid));
        }
        return $row;
    }
    //kuerux bat
    public static function news_tag($id,$where=''){
        $row=M("article")
            ->join("dp_articletag ON dp_article.id=dp_articletag.aid")
            ->where("dp_article.id='$id' ".$where)
            ->getField("tid",true);
        if($row){
            $row=implode(",",$row);
            $tags=M("tag")->where("id IN ($row)")->select();
            return $tags;
        }
    }
    public static function news_favour($id){
        $row=M("article")->where("id='$id'")->getField("favour",true);
        return $row[0];
    }
    //manba
    public static function source($id,$where=''){
        $row=M("article")
            ->where("dp_article.id='$id' ".$where)
            ->getField("from_source",true);
        $source=M("source")->where("id='$row[0]'")->select();
        return $source;
    }

    public static function view_news($id,$where='',$limit0="",$limit1="",$fields='',$order){
        //qong tur
        $user=M("article");
        $pid=$user->where("id='$id'")->getField('pid',true);
        $row=self::home_news($pid[0],$where,$limit0,$limit1,$fields,"",$order);
        $umum_hawar_san=count($row);//9
        if($limit1>$umum_hawar_san){
             $ids=explode(",",Home2018Controller::getAllFcateIds($pid[0]));
             //分别根父级id
             $ids=array_slice($ids,0,count($ids)-1);
             $row1=self::home_news($ids[0],"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
             $rows=array_merge($row,$row1);
            if(count($rows)<$limit1){
                $row1=self::home_news($ids[1],"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
                $rows=array_merge($row,$row1);
                if(count($rows)<$limit1){
                    $row1=self::home_news($ids[2],"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
                    $rows=array_merge($row,$row1);
                    if(count($rows)<$limit1){
                        if($ids[3]){
                            $row1=self::home_news($ids[3],"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
                            $rows=array_merge($row,$row1);
                        }
                        if(count($rows)<$limit1){
                            foreach ($ids as $v){
                                $row1=self::home_news($v,"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
                            }
                            $rows=array_merge($row,$row1);
                            if(count($rows)<$limit1){
                                //分别根父级id
                                $ids=explode(",",Home2018Controller::getAllFcateIds($pid[0]));
                                foreach ($ids as $v){
                                    $row1=self::home_news($v,"and pid not in ($pid[0])".$where,$limit0,$limit1-$umum_hawar_san,$fields,"",$order);
                                }
                                $rows=array_merge($row,$row1);
                            }
                        }
                    }
                }
            }
            return $rows;
        }else{
            return $row;
        }
    }
    //tur ratlax
    public static function common_tur($url){
        return $url;
    }
    //yalhuz hawarga erixix
    public static function article($pid){
        $catids=Home2018Controller::getAllChildcateIds($pid);
        if($catids){
            $row=M("article")->where("status=1 and pid IN ($catids) ")
                ->field("id")
                ->select();
        }else{
            $row=M("article")->where("status=1 and pid=".$pid)
                ->field("id")
                ->select();
        }
        return $row;
    }
    public static function category($id=0,$url,$wehere='',$order="sort asc",$boll){
        //qong tur
        $map=array();
        $row_big=M("cate")->where("status=1 and pid=".$id .$wehere)
            ->field("id,name,status,sort,pid")
            ->order($order)
            ->select();
        foreach ($row_big as $row){
            if($id==$row['pid']){
                $row['url']="/".$url."/".$row['id'].'/1'.self::$url;
                $row['list']=self::category($row['id']);
                //iqida tur bommisa dadisingi id sini al

//                $news=self::article($row['pid']);
//                if(!$row['list'] and $boll!=false and !$news){
//                    $row['url']="/".$url."/".$row['pid'].'/1'.self::$url;
//                }
                //iqida tur bommisa none ni ketiwat
                if(!$row['list']){
                    $row['display']="none";
                }
                $map[]=$row;
            }
        }
        return $map;
    }
    public static function _category_id_url($id=0,$url='',$boll=true){
        return self::category($id,$url,"","",$boll);
    }

    //yalhuz birla ana categoryni qikirix
    public static function _category_bala_ana_cat($id,$url){
        $row=M("cate")->where("id=".$id)->find();
        $row['url']="/".$url."/".$row['id'].'/1'.self::$url;
        return $row;
    }
    //bala sahipining yalhuz ania sahipa
    public static function _category_bala_ana_id($id,$url){
        return self::_category_bala_ana_cat($id,$url);
    }
    //okuxluk
    public static function okuxluk_tur($catid,$where=''){
        //qong tur
        $map=array();
        $row=M("article")->where("status=1 and pid=".$catid .$where)
            ->field("id,title,status,intime,thumb")
            ->order('intime desc')
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/view/".date("Y/m/",$v['intime']).$v['id'].self::$url;
            $map[]=$v;
        }
        return $map;
    }
    //person
    public static function person_cat($pid,$where=''){
        //qong tur
        $map=array();
        $row=M("cate")->where("status=1 and pid=".$pid .$where)
            ->field("id,name,status,sort")
            ->order('sort asc')
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/personlist/".$v['id'].'/1'.self::$url;
            $map[]=$v;
        }
        return $map;
    }
//contact
    public static function contact_cat($pid,$where=''){
        //qong tur
        $map=array();
        $row=M("cate")->where("status=1 and pid=".$pid .$where)
            ->field("id,name,status,sort")
            ->order('sort asc')
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/contactlist/".$v['id'].'/1'.self::$url;
            $map[]=$v;
        }
        return $map;
    }
    //law
    public static function law_cat($pid,$where=''){
        //qong tur
        $map=array();
        $row=M("cate")->where("status=1 and pid=".$pid .$where)
            ->field("id,name,status,sort")
            ->order('sort asc')
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/lawlist/".$v['id'].'/1'.self::$url;
            $map[]=$v;
        }
        return $map;
    }
    //law
    public static function shop_cat($pid,$where=''){
        //qong tur
        $map=array();
        $row=M("cate")->where("status=1 and pid=".$pid .$where)
            ->field("id,name,status,sort")
            ->order('sort asc')
            ->select();
        foreach ($row as $k=>$v){
            $v['url']="/shoplist/".$v['id'].'/1'.self::$url;
            $map[]=$v;
        }
        return $map;
    }




    //inkas
    public static function comment($pid,$where=''){
        //qong tur
        $row=M("comment")->where("status=1 and nolisit='ok' and newsid=".$pid .$where)
            ->field("id,isim,status,content,intime,face")
            ->order('intime asc')
            ->select();
        return $row;
    }
    //处理带图片的广告
    public static function ads($id){
        if(!S("elan".$id)){
            $ad=M("bannerad")->where("status=1 and id=".$id)->select();
            //写缓存  一个星期
            S("elan".$id,$ad,604800);
        }else{
            $ad=S("elan".$id);
        }
        $url=$ad[0]['adurl']?$ad[0]['adurl']:"##";
        $target=$ad[0]['adurl']?"target='_blank'":"target='_self'";
        echo "<a href='$url' $target ><img src="."/"._strpos($ad[0]['thumb'])."></a>";
    }
}